package com.aboo.zookeeper.action;

import com.aboo.frame.constant.FlagEnum;
import com.aboo.frame.vo.ReturnVO;
import com.aboo.zookeeper.service.ZookeeperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/zookeeper")
public class ZookeeperAction {

    @Resource
    private ZookeeperService zookeeperService;

    @RequestMapping("/saveNode")
    public ReturnVO<Object> saveNode(String path, String data) {
        ReturnVO<Object> rv = new ReturnVO<>();
        boolean flag = zookeeperService.create(path, data);
        if (flag) {
            rv.setFlag(FlagEnum.SUCCESS);
        } else {
            rv.setFlag(FlagEnum.FAILURE);
        }

        return rv;
    }

    @RequestMapping("/deleteNode")
    public ReturnVO<Object> deleteNode(String path) {
        ReturnVO<Object> rv = new ReturnVO<>();
        boolean flag = zookeeperService.delete(path);
        if (flag) {
            rv.setFlag(FlagEnum.SUCCESS);
        } else {
            rv.setFlag(FlagEnum.FAILURE);
        }

        return rv;
    }

    @RequestMapping("/queryNode")
    public ReturnVO<Object> queryNode(String path) {
        ReturnVO<Object> rv = new ReturnVO<>();
        String data = zookeeperService.query(path);

        rv.setData(data);
        return rv;
    }

    @RequestMapping("/modifyNode")
    public ReturnVO<Object> modifyNode(String path, String data) {
        ReturnVO<Object> rv = new ReturnVO<>();
        boolean flag = zookeeperService.update(path, data);
        if (flag) {
            rv.setFlag(FlagEnum.SUCCESS);
        } else {
            rv.setFlag(FlagEnum.FAILURE);
        }

        return rv;
    }


}
